package model;

import entities.User;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class EditUserLoginTableModel extends AbstractTableModel {

    ArrayList<User> list;
    ArrayList<User> disp;

    public EditUserLoginTableModel() {
        list = new ArrayList<User>();
        disp = new ArrayList<User>();
    }

    public EditUserLoginTableModel(ArrayList<User> list) {
        this.list = new ArrayList<User>(list);
        this.disp = new ArrayList<User>(list);
    }

    @Override
    public int getRowCount() {
        return disp.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        User user = disp.get(rowIndex);
        String[] status = {"open","lock"};
        String[] cPass = {"available", "null"};
        switch (columnIndex) {
            case 0:
                return user.getLoginID();
            case 1:
                return user.getTitle();
            case 2:
                return status[user.getStatus() - 1];
            case 3:
                if(user.getLoginPass() != null && user.getLoginPass().length() != 0) {
                    return cPass[0];
                }
                return cPass[1];
            case 4:
                return user.getPhone();
            case 5:
                return user.getEmailAddress();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Login";
            case 1:
                return "Title";
            case 2:
                return "Status";
            case 3:
                return "Password";
            case 4:
                return "Phone";
            case 5:
                return "Email Address";
            default:
                return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public void addAll(ArrayList<User> list) {
        this.list.addAll(list);
        this.disp.addAll(list);
        fireTableDataChanged();
    }

    public void addElement(User user) {
        list.add(user);
        disp.add(user);
        fireTableDataChanged();
    }

    public User getAt(int index) {
        return disp.get(index);
    }

    public void removeAt(int index) {
        list.remove(index);
    }

    public void update(int index, User user) {
        list.set(index, user);
        fireTableDataChanged();
    }
    
    public void update(User user) {
        list.set(list.indexOf(user), user);
        int index = disp.indexOf(user);
        if(index != -1) {
            disp.set(index, user);
        }
        fireTableDataChanged();
    }

    public void clear() {
        list.clear();
    }

    public void fillter(String loginID, String name, int departmentID, int role, int level) {
        disp.clear();
        for (User u : list) {
            boolean accept = true;
            if (loginID != null && loginID.length() != 0) {
                int length = loginID.length();
                if (u.getLoginID().length() >= length) {
                    if (!u.getLoginID().substring(0, length).equalsIgnoreCase(loginID)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (name != null && name.length() != 0) {
                int length = name.length();
                if (u.getLoginID().length() >= length) {
                    if (!u.getName().substring(0, length).equalsIgnoreCase(name)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (departmentID != 0) {
                if (u.getDepartmentID() != departmentID) {
                    accept = false;
                }
            }
            if (role != 0) {
                if (u.getRole() != role) {
                    accept = false;
                }
            }
            if (level != 0) {
                if (u.getLevel() != level) {
                    accept = false;
                }
            }
            if (accept) {
                disp.add(u);
            }
        }
        fireTableDataChanged();
    }
}
